/*
 * Created on 2004-11-16
 *
 * Copyright: Bluejay Networks Inc. & Inquiry Management Systems Ltd.  2004
 *
 * This software is the proprietary information of Bluejay Networks Inc. & 
 * Inquiry Management Systems Ltd.  
 * Use is subject to license terms.
 */
package com.ims.iaa.JavaScript;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Timestamp;
import com.ims.iaa.*;

/** 
*
* <p>This class <code>RemoteJSSaverImpl</code> provides the Script Save 
* services for other classes. This class is act as an remote RMI services 
* object which implements the <code>RemoteJSSaver</code> remote 
* interface.</p>
*
* @see RemoteJSSaver
*
* @author ShengMeng Bai
*
* @version 1.00, 16 Nov. 2004
* 
* @author changsheng 
* @version 2.5 ,2006-7-21 for js cache
*/
public class RemoteJSSaverImpl extends UnicastRemoteObject implements RemoteJSSaver {
	// the single one object
	private static RemoteJSSaverImpl m_singleObject = null;
	
	private PrintStream m_out = null;
	private long m_iSaverSize = 0;
	private long MAX_SAVER_SIZE = 10240*1024;//10M
	private boolean m_bSaveToFile = IaaDebug.AuxDebug;//updated by yxy on 2009-02-04
	
	private RemoteJSSaverImpl() throws RemoteException{
	}
	 
 	private void createFile() {
		String fileName = getFileName();
		String backupName = fileName.replaceFirst(".txt", "_old.txt");

		//base on user setting, make full path name.
		if (!IaaDebug.LogDirectory.equals("")) {
		 	File path = new File(IaaDebug.LogDirectory);
 			try  {
 				if (!path.exists())	path.mkdirs();

 				fileName = IaaDebug.LogDirectory + "\\" + fileName;
 				backupName = IaaDebug.LogDirectory + "\\" + backupName;
		 	} catch (SecurityException e)  {
 				IaaUtil.log("Create dir error " + path + " error: " + e);			
	 		}		
		}
		
		try  {
			//++if file size larger than MAX_SAVER_SIZE, create new.
			//IaaUtil.log("yxy===============yxy");
			//IaaUtil.log("fileName is "+ fileName);
			//IaaUtil.log("backupName is "+ backupName);
			File saver = new File(fileName);//updated by yxy on 2009-02-04
			File backup = new File(backupName);//updated by yxy on 2009-02-04
			IaaUtil.log("saver.exists() = "+String.valueOf(saver.exists()));
			if(saver.exists()){
				//if file size > 10M, backup it and create new
				m_iSaverSize = saver.length();
				//IaaUtil.log("TEST IN CREATE FILE ================");
				//IaaUtil.log("m_iSaverSize == "+m_iSaverSize);
				//IaaUtil.log("MAX_SAVER_SIZE == "+ MAX_SAVER_SIZE);
				if (m_iSaverSize > MAX_SAVER_SIZE){
					if(backup.exists()){
						backup.delete();
					}
					saver.renameTo(backup);
					IaaUtil.log("The Script file size bigger than " +MAX_SAVER_SIZE + "Create new one.\nThe old file had been rename.");
					m_iSaverSize = 0;
				}
			}
			//--if file size larger than MAX_SAVER_SIZE, create new.
			m_out = new PrintStream( new FileOutputStream(fileName, true));
			//m_bSaveToFile = true;
		} catch (IOException e ) {
			//m_bSaveToFile = false;
			System.out.println( "Create Script Saver failed " + fileName + " :" + e );
			m_out = System.out;
		}
 	}
	
	private String getFileName() {
	    String sRet = "";
		try  {
		    sRet = java.net.InetAddress.getLocalHost().getHostName();
		} catch (Exception e)  {
			IaaUtil.log("Error get localhost name" + e);
			sRet = "Unknown";
		}
		sRet += "_ErrorScript.txt";
		
		return sRet;
	}
	
	private void closeFile()  {
		if(m_bSaveToFile){
			m_out.close();
		}
	}
	
	public static RemoteJSSaver getInstance(){
		if(m_singleObject == null){
			try{
				m_singleObject = new RemoteJSSaverImpl();
				m_singleObject.createFile();
			}catch (Exception e){
				IaaUtil.log("Can't create Script Saver: " + e.toString());
			}
		}
		return m_singleObject;
	}
	/* (non-Javadoc)
	 * @see com.ims.iaa.JavaScript.RemoteJSSaver#saveScript(java.lang.String, java.lang.String)
	 */
	public void saveScript(String sScript, String sMessage, String sContainer) throws RemoteException{
		synchronized(this) {
			//IaaUtil.log("TEST=================TEST");
			//IaaUtil.log("m_bSaveToFile is "+m_bSaveToFile);
			//IaaUtil.log("m_iSaverSize is "+m_iSaverSize);
			if(m_bSaveToFile){
				if(m_iSaverSize > MAX_SAVER_SIZE){
					closeFile();
					m_iSaverSize = 0;
					createFile();
				}
			}
		}
		if(m_bSaveToFile){
			String time = new Timestamp(System.currentTimeMillis()).toString() + " in " + sContainer;
			IaaUtil.log(time + ": ");//added by yxy on 2009-02-04
			IaaUtil.log(sMessage);
			m_iSaverSize += time.length() + 4;
			m_iSaverSize += sMessage.length();
			m_iSaverSize += sScript.length();
			
			m_out.println(time + ": ");
			m_out.println(sMessage);
			m_out.println(sScript);
			m_out.println();
		}
	}

	/* (non-Javadoc)
	 * @see com.ims.iaa.JavaScript.RemoteJSSaver#saveScript(java.lang.String)
	 */
	public void saveScript(String sScript, String sContainer) throws RemoteException{
		synchronized(this) {
			if(m_bSaveToFile){
				if(m_iSaverSize > MAX_SAVER_SIZE){
					closeFile();
					m_iSaverSize = 0;
					createFile();
				}
			}
		}
		if(m_bSaveToFile){			
			String time = new Timestamp(System.currentTimeMillis()).toString() + " in " + sContainer;
			m_iSaverSize += time.length() + 4;
			m_iSaverSize += sScript.length();
			
			m_out.println(time + ": ");
			m_out.println(sScript);
			m_out.println();
		}
	}
}
